package leetcode.String;


/**
 * Created by Admin on 2020/7/26.
 */
public class Question557 {

    static public String reverseWords(String s) {
        String[] strs = s.split(" ");
        for (int i =0;i<strs.length;i++){
            String item = strs[i];
            StringBuilder StringBuilder = new StringBuilder();
            char[] chars = item.toCharArray();
            for (int j = chars.length-1;j>=0;j--){
                StringBuilder.append(chars[j]);
            }
            strs[i] = StringBuilder.toString();
         }
        String result = "";
        for (int i= 0;i<strs.length;i++){
            result+=strs[i]+" ";
        }
        return result.substring(0,result.length()-1);
    }

    static public String reverseWords1(String s) {
        String[] strings = s.split(" ");
        StringBuilder stringBuilder = new StringBuilder();
        for (String word : strings){
            stringBuilder.append(new StringBuffer(word).reverse()+" ");
        }
        return stringBuilder.toString().trim();
    }


    //测试了stringbuffer和string发现stringbuffer确实要比string省内存空间
    public static void main(String[] args) {
        String s = "Let's take LeetCode contest";
        System.out.println(reverseWords1(s));

    }
}
